package com.bjtu.laorenyouhao.laoren.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bjtu.common.valid.group.AddGroupp;
import com.bjtu.common.valid.group.UpdateGroup;
import com.bjtu.common.valid.group.UpdateStatusGroup;
import com.bjtu.laorenyouhao.laoren.entity.JianhurenEntity;
import com.bjtu.laorenyouhao.laoren.entity.LaorenJianhurenEntity;
import com.bjtu.laorenyouhao.laoren.service.JianhurenService;
import com.bjtu.laorenyouhao.laoren.service.LaorenJianhurenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bjtu.laorenyouhao.laoren.entity.LaorenInfoEntity;
import com.bjtu.laorenyouhao.laoren.service.LaorenInfoService;
import com.bjtu.common.utils.PageUtils;
import com.bjtu.common.utils.R;


/**
 * 老人基本信息
 *
 * @author yangyalong
 * @email yangyalong@gmail.com
 * @date 2021-07-08 20:27:26
 */
@RestController
@RequestMapping("laoren/laoreninfo")
public class LaorenInfoController {
    @Autowired
    private LaorenInfoService laorenInfoService;
    @Autowired
    private JianhurenService jianhurenService;
    @Autowired
    private LaorenJianhurenService laorenJianhurenService;

    //根据名字获取id
    @RequestMapping("/getId")
    public R getLaorenId(@RequestParam String name){
        LaorenInfoEntity laoren = laorenInfoService.getOne(new QueryWrapper<LaorenInfoEntity>().eq("name", name));
        return R.ok().put("laorenId",laoren.getId());
    }

    @RequestMapping("/addJianhuren")
    public R addjianhuren(@RequestBody Map<String,Object> params){
        JianhurenEntity jianhurenEntity = new JianhurenEntity();
        jianhurenEntity.setName((String) params.get("name"));
        jianhurenEntity.setPhone((String) params.get("phone"));
        jianhurenEntity.setWeixin((String) params.get("weixin"));
        jianhurenService.save(jianhurenEntity);

        LaorenJianhurenEntity laorenJianhurenEntity = new LaorenJianhurenEntity();
        laorenJianhurenEntity.setDescription((String) params.get("shuoming"));
        laorenJianhurenEntity.setLaorenId(Long.valueOf((Integer) params.get("laorenId")));
        laorenJianhurenEntity.setJianhurenId(jianhurenEntity.getId());
        laorenJianhurenEntity.setJianhurenType((Integer) params.get("jhtype"));
        laorenJianhurenService.save(laorenJianhurenEntity);

        System.out.println(params);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = laorenInfoService.queryPage(params);
        System.out.println(page);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        LaorenInfoEntity laorenInfo = laorenInfoService.getById(id);

        return R.ok().put("laorenInfo", laorenInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody LaorenInfoEntity laorenInfo) {
        System.out.println(laorenInfo);
        laorenInfo.setCheckinDate(new Date());
        laorenInfo.setIsRemove(1);
        laorenInfo.setProfilePhoto("https://laorenyouhao.oss-cn-beijing.aliyuncs.com/" + laorenInfo.getProfilePhoto());
        laorenInfo.setImgsetDir(laorenInfo.getImgsetDir());
        laorenInfoService.save(laorenInfo);
        return R.ok();
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@Validated(UpdateGroup.class) @RequestBody LaorenInfoEntity laorenInfo) {
        laorenInfoService.updateById(laorenInfo);

        return R.ok();
    }

    @RequestMapping("/update/volubteer")
    public R updateVoulunteer(@Validated(UpdateStatusGroup.class) @RequestBody LaorenInfoEntity laorenInfo) {
        System.out.println(laorenInfo);
        laorenInfoService.updateById(laorenInfo);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        laorenInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
